package com.rui.study.algorithm.L_深入拆解JVM_反射;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

// -Djava.lang.Integer.IntegerCache.high=128
// -Dsun.reflect.noInflation=true
public class FooV4 {

    public static void target(int i) {
//        new Exception().printStackTrace();
    }


    private static void reflectInvokeTest() {
        try {
            Class cls = Class.forName("com.rui.study.algorithm.L_深入拆解JVM_反射.FooV4");
            Method method = cls.getMethod("target", int.class);

            method.setAccessible(true);     // 关闭权限检查

            long current = System.currentTimeMillis();
            for (int i = 1; i <= 2_000_000_000; i++) {
                if (i % 100_000_000 == 0) {
                    long temp = System.currentTimeMillis();
                    System.out.println(temp - current);
                    current = temp;
                }
                method.invoke(null, 128);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private static void directInvokeTest() {
        long current = System.currentTimeMillis();
        for (int i = 1; i <= 2_000_000_000; i++) {
            if (i % 100_000_000 == 0) {
                long temp = System.currentTimeMillis();
                System.out.println(temp - current);
                current = temp;
            }
            FooV4.target(128);
        }
    }

    public static void main(String[] args) {
//        directInvokeTest();        // 160ms
        reflectInvokeTest();    // 144ms
    }
}
